package org.springblade.tms.module.userinfo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.system.user.feign.IUserClient;
import org.springblade.tms.global.entity.TimeInterval;
import org.springblade.tms.global.enums.ApprovalStatusEnum;
import org.springblade.tms.module.car.entity.TMCar;
import org.springblade.tms.module.supercargoinfo.entity.TMSupercargoInfo;
import org.springblade.tms.module.supercargoinfo.vo.TMSupercargoInfoVO;
import org.springblade.tms.module.tank.entity.TMTankTank;
import org.springblade.tms.module.tank.vo.TMTankTankVO;
import org.springblade.tms.module.userinfo.base.CarrierManagementService;
import org.springblade.tms.module.userinfo.dto.CarDto;
import org.springblade.tms.module.userinfo.dto.CarrierAuditReq;
import org.springblade.tms.module.userinfo.dto.DriverDto;
import org.springblade.tms.module.userinfo.entity.TMUserInfo;
import org.springblade.tms.module.userinfo.service.IUserInfoService;
import org.springblade.tms.module.userinfo.vo.CarrierGroupVo;
import org.springblade.tms.module.userinfo.vo.TMUserInfoVO;
import org.springblade.tms.module.userinfo.vo.UserInfoVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author yr
 * @Date 2022/3/4 18:14
 * @Desc 承运商管理相关接口
 */
@RestController
@RequestMapping("userInfo/CarrierManagement")
@Api(value = "承运商管理-yr", tags = "承运商管理-yr")
public class CarrierManagementController {
	@Resource
	private CarrierManagementService carrierManagementService;
	@Resource
	private IUserClient userClient;
	@Resource
	private IUserInfoService userInfoService;

	/**
	 * @return
	 * @Desc 承运商信息查询
	 * @author yr
	 * @Date 2022/3/7 11:14
	 */
	@ApiLog("承运商信息查询")
	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "承运商信息查询", notes = "传入userInfo")
	public R<IPage<UserInfoVo>> list(TMUserInfoVO userInfo, Query query, TimeInterval timeInterval) {
		return carrierManagementService.list(userInfo, query, timeInterval);
	}

	/**
	 * @return
	 * @Desc 承运商个人信息查询
	 * @author yr
	 * @Date 2022/3/7 11:14
	 */
	@ApiLog("承运商个人信息查询")
	@GetMapping("/personalInformation")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "承运商个人信息查询")
	public R<UserInfoVo> personalInformation() {
		return carrierManagementService.personalInformation();
	}

	/**
	 * @return
	 * @Desc 承运商个人信息修改
	 * @author yr
	 * @Date 2022/3/7 11:14
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "承运商个人信息修改", notes = "传入userInfo")
	@ApiLog("承运商个人信息修改")
	public R update(@Valid @RequestBody TMUserInfo userInfo) {
		return R.status(carrierManagementService.update(userInfo));
	}

	/**
	 * @return
	 * @Desc 承运商资质审查列表
	 * @author yr
	 * @Date 2022/3/7 11:14
	 */
	@ApiLog("承运商资质审查列表")
	@GetMapping("/qualificationReview")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "承运商资质审查列表", notes = "传入userInfo")
	public R<IPage<UserInfoVo>> qualificationReview(TMUserInfoVO userInfo, Query query, TimeInterval timeInterval) {
		userInfo.setApprovalStatus(ApprovalStatusEnum.PENDING_REVIEW.getValue());
		return carrierManagementService.list(userInfo, query, timeInterval);
	}

	/**
	 * @return
	 * @Desc 承运商审核
	 * @author yr
	 * @Date 2022/3/7
	 */
	@ApiLog("承运商审核")
	@PostMapping("/check")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "承运商审核", notes = "传入carrierAuditReq")
	public R check(@RequestBody List<CarrierAuditReq> carrierAuditReq) {
		return carrierManagementService.check(carrierAuditReq);
	}


	@ApiLog("司机列表查询")
	@GetMapping("/driverList")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "司机列表查询", notes = "driverDto")
	public R<IPage<TMUserInfoVO>> driverList(DriverDto driverDto, Query query, TimeInterval timeInterval) {
		return carrierManagementService.driverList(driverDto, query, timeInterval);
	}

	@ApiLog("车辆列表查询")
	@PostMapping("/carList")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "车辆列表查询", notes = "carDto")
	public R<IPage<TMCar>> carList(@RequestBody CarDto carDto, Query query) {
		return carrierManagementService.carList(carDto, query);
	}

	@ApiLog("押运员列表查询")
	@GetMapping("/escortList")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "押运员列表查询", notes = "tmSupercargoInfo")
	public R<IPage<TMSupercargoInfo>> escortList(TMSupercargoInfoVO tmSupercargoInfo, Query query, TimeInterval timeInterval) {
		return carrierManagementService.escortList(tmSupercargoInfo, query, timeInterval);
	}

	@ApiLog("坦克罐列表查询")
	@GetMapping("/tankList")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "坦克罐列表查询", notes = "tankVo")
	public R<IPage<TMTankTank>> escortList(TMTankTankVO tankVo, Query query) {
		return carrierManagementService.tankList(tankVo, query);
	}

	@ApiLog("重置密码")
	@PostMapping("/resetPassword")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "重置密码", notes = "parentId")
	public R resetPassword(@RequestBody String parentId) {
		Object id = JsonUtil.parse(parentId, Map.class).get("parentId");
		R<Boolean> r = userClient.resetPassword(String.valueOf(id));
		return r;
	}

	@ApiLog("分配组别")
	@PostMapping("/assignGroup")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "分配组别", notes = "parentId")
	public R assignGroup(@RequestBody CarrierGroupVo carrierGroupVo) {
		TMUserInfo tmUserInfo = userInfoService.getById(carrierGroupVo.getId());
		tmUserInfo.setCarrierGroup(carrierGroupVo.getGroup());
		return R.status(userInfoService.updateById(tmUserInfo));
	}
}
